package com.warm.pump.frame.gen.util;

import com.warm.pump.frame.util.ArrayUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TypeCast {

    public static String getString(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof String) {
            return (String) obj;
        }
        if (obj instanceof Timestamp) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Timestamp date = (Timestamp) obj;
            return sdf.format(date);
        }
        return String.valueOf(obj);
    }

    public static Integer getInteger(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Integer) {
            return (Integer) obj;
        } else if (obj instanceof String) {
            return Integer.parseInt((String) obj);
        }
        if (obj instanceof BigDecimal) {
            return ((BigDecimal) obj).intValue();
        }
        if (obj instanceof Double) {
            return new Double((Double) obj).intValue();
        } else {

            return Integer.parseInt(obj.toString());
        }
    }

    public static Integer getInteger(Object obj, int defaultInt) {
        Integer rt = getInteger(obj);
        if (rt == null) {
            rt = defaultInt;
        }
        return rt;
    }

    public static Long getLong(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof String) {
            return Long.parseLong((String) obj);
        } else if (obj instanceof BigDecimal) {
            BigDecimal objBd = (BigDecimal) obj;
            return objBd.longValue();
        } else {
            return Long.parseLong(obj.toString());
        }
    }

    public static BigDecimal getBigDecimal(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        } else if (obj instanceof Double) {
            BigDecimal objBd = new BigDecimal(Double.toString((Double) obj));
            return objBd;
        } else if (obj instanceof String) {
            BigDecimal objBd = new BigDecimal((String) obj);
            return objBd;
        } else {
            return null;
        }
    }

    public static BigDecimal getBigDecimal(Object obj, int scale) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof BigDecimal) {
            BigDecimal objBd = (BigDecimal) obj;

            String objStr = objBd.stripTrailingZeros().toPlainString();
            objBd = new BigDecimal((String) objStr);
            objBd.setScale(scale, RoundingMode.HALF_UP);
            return objBd;
        } else if (obj instanceof Double) {

            String objStr = BigDecimal.valueOf((Double) obj).stripTrailingZeros().toPlainString();
            BigDecimal objBd = new BigDecimal((String) objStr);
            objBd.setScale(scale, RoundingMode.HALF_UP);
            return objBd;
        } else if (obj instanceof String) {
            BigDecimal objBd = new BigDecimal((String) obj);
            objBd.stripTrailingZeros().setScale(scale, RoundingMode.HALF_UP);
            return objBd;
        } else {
            return null;
        }
    }

    public static BigDecimal getBigDecimalDefultZero(Object obj) {
        if (obj == null) {
            return BigDecimal.ZERO;
        }
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        } else if (obj instanceof String) {
            BigDecimal objBd = new BigDecimal((String) obj);
            return objBd;
        } else {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal getBigDecimalMinZero(BigDecimal bd) {
        if (bd == null) {
            return BigDecimal.ZERO;
        }
        if (bd.compareTo(BigDecimal.ZERO) <= 0) {
            return BigDecimal.ZERO;
        } else {
            return bd;
        }

    }


    public static Date getDate(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Date) {
            return (Date) obj;
        } else if (obj instanceof String) {
            String objStr = (String) obj;
            String sdf = "yyyy-MM-dd";
            if (objStr.indexOf(":") != -1) {
                sdf = "yyyy-MM-dd HH:mm:ss";
            }
            try {
                DateFormat objDf = new SimpleDateFormat(sdf);
                Date objDate = objDf.parse(objStr);
                return objDate;
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static List<String> getStringList(Object obj) {
        if (obj == null) {
            return null;
        }
        List<String> retList = null;
        if (obj instanceof List) {
            List<String> objList = (List<String>) obj;
            if (objList.size() < 1) {
                return null;
            }
            return objList;
        } else if (obj instanceof String) {
            String objStr = (String) obj;
            List<String> commaList = ArrayUtils.getListByComma(objStr);
            return commaList;
        } else {
            retList.add(String.valueOf(obj));
            return retList;
        }
    }

    public static List<Integer> getIntegerList(Object obj) {
        if (obj == null) {
            return null;
        }
        List<Integer> retList = null;
        if (obj instanceof List) {
            return (List<Integer>) obj;
        } else if (obj instanceof String) {
            String objStr = (String) obj;
            retList = new ArrayList<Integer>();
            List<String> commaList = ArrayUtils.getListByComma(objStr);
            for (String string : commaList) {
                retList.add(getInteger(string));
            }
            return retList;
        } else if (obj instanceof Integer) {
            retList.add((Integer) obj);
            return retList;
        } else {
            return null;
        }
    }

    public static List<Long> getLongList(Object obj) {
        if (obj == null) {
            return null;
        }
        List<Long> retList = null;
        if (obj instanceof List) {
            return (List<Long>) obj;
        } else if (obj instanceof String) {
            String objStr = (String) obj;
            retList = new ArrayList<Long>();
            List<String> commaList = ArrayUtils.getListByComma(objStr);
            for (String string : commaList) {
                retList.add(getLong(string));
            }
            return retList;
        } else if (obj instanceof Long) {
            retList.add((Long) obj);
            return retList;
        } else {
            return null;
        }
    }

    public static List<BigDecimal> getBigDecimalList(Object obj) {
        if (obj == null) {
            return null;
        }
        List<BigDecimal> retList = null;
        if (obj instanceof List) {
            return (List<BigDecimal>) obj;
        } else if (obj instanceof String) {
            String objStr = (String) obj;
            List<String> commaList = ArrayUtils.getListByComma(objStr);
            for (String string : commaList) {
                retList.add(getBigDecimal(string));
            }
            return retList;
        } else if (obj instanceof BigDecimal) {
            retList.add((BigDecimal) obj);
            return retList;
        } else {
            return null;
        }
    }

    public static List<Date> getDateList(Object obj) {
        if (obj == null) {
            return null;
        }
        List<Date> retList = null;
        if (obj instanceof List) {
            return (List<Date>) obj;
        } else if (obj instanceof String) {
            String objStr = (String) obj;
            List<String> commaList = ArrayUtils.getListByComma(objStr);
            for (String string : commaList) {
                retList.add(getDate(string));
            }
            return retList;
        } else if (obj instanceof Date) {
            retList.add((Date) obj);
            return retList;
        } else {
            return null;
        }
    }


    public static Object getObjectWithDateFormat(Object obj) {
        if (obj instanceof Date && "java.sql.Date".equals(obj.getClass().getName())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = (Date) obj;
            return sdf.format(date);
        } else if (obj instanceof Timestamp && "java.sql.Timestamp".equals(obj.getClass().getName())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Timestamp date = (Timestamp) obj;
            return sdf.format(date);
        }

        return obj;
    }
}
